<?php
// 直接使用PDO连接数据库，查询订单57的物料计算情况

// 数据库配置
$host = 'localhost';
$dbname = 'scanwork';
$username = 'scanwork';
$password = 'rDezxK9h7tWp5sP7'; // 根据实际情况修改

try {
    // 创建数据库连接
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // 定义要查询的订单ID
    $orderId = 57;
    
    echo "\n=== 订单 {$orderId} 物料计算情况查询 ===\n\n";
    
    // 1. 查询订单基本信息
    $stmt = $pdo->prepare("SELECT * FROM fa_scanwork_order WHERE id = ?");
    $stmt->execute([$orderId]);
    $order = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$order) {
        echo "订单 {$orderId} 不存在！\n";
        exit;
    }
    
    echo "订单信息：\n";
    echo "- 订单编号：{$order['order_no']}\n";
    echo "- 订单名称：{$order['order_name']}\n";
    echo "- 客户名称：{$order['customer_name']}\n";
    echo "- 创建时间：".date('Y-m-d H:i:s', $order['createtime'])."\n";
    echo "- 订单总数量：{$order['total_quantity']}\n\n";
    
    // 2. 查询订单包含的产品型号
    $stmt = $pdo->prepare("SELECT om.*, pm.name AS model_name, p.name AS product_name 
                           FROM fa_scanwork_order_model om 
                           LEFT JOIN fa_scanwork_product_model pm ON om.model_id = pm.id 
                           LEFT JOIN fa_scanwork_product p ON pm.product_id = p.id 
                           WHERE om.order_id = ?");
    $stmt->execute([$orderId]);
    $orderModels = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "订单包含的产品型号：\n";
    if (count($orderModels) > 0) {
        foreach ($orderModels as $index => $model) {
            echo "- [型号{$index}] {$model['product_name']} - {$model['model_name']}：{$model['quantity']}件\n";
        }
    } else {
        echo "- 暂无产品型号\n";
    }
    echo "\n";
    
    // 3. 查询每个型号的工序物料配置
    echo "各型号的工序物料配置：\n";
    if (count($orderModels) > 0) {
        foreach ($orderModels as $model) {
            $modelId = $model['model_id'];
            $modelName = $model['model_name'];
            
            $stmt = $pdo->prepare("SELECT pm.*, p.name AS process_name, m.name AS material_name 
                                   FROM fa_scanwork_process_material pm 
                                   LEFT JOIN fa_scanwork_process p ON pm.process_id = p.id 
                                   LEFT JOIN fa_scanwork_material m ON pm.material_id = m.id 
                                   WHERE pm.model_id = ? AND pm.status = 1");
            $stmt->execute([$modelId]);
            $processMaterials = $stmt->fetchAll(PDO::FETCH_ASSOC);
            
            echo "型号 {$modelId} ({$modelName}) 共有 ".count($processMaterials)." 个工序物料配置：\n";
            if (count($processMaterials) > 0) {
                foreach ($processMaterials as $pm) {
                    echo "  - 工序：{$pm['process_name']}，物料：{$pm['material_name']}，单件用量：{$pm['quantity']}，损耗率：{$pm['loss_rate']}%\n";
                }
            } else {
                echo "  - 暂无工序物料配置\n";
            }
        }
    } else {
        echo "- 订单无产品型号，无法查询工序物料配置\n";
    }
    echo "\n";
    
    // 4. 查询订单物料需求结果
    $stmt = $pdo->prepare("SELECT om.*, m.name AS material_name, s.name AS supplier_name 
                           FROM fa_scanwork_order_material om 
                           LEFT JOIN fa_scanwork_material m ON om.material_id = m.id 
                           LEFT JOIN fa_scanwork_supplier s ON om.supplier_id = s.id 
                           WHERE om.order_id = ?");
    $stmt->execute([$orderId]);
    $orderMaterials = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "订单物料需求计算结果：\n";
    echo "共有 ".count($orderMaterials)." 个物料需求记录\n";
    if (count($orderMaterials) > 0) {
        foreach ($orderMaterials as $om) {
            echo "- 物料：{$om['material_name']}，需求数量：{$om['required_quantity']}，预估单价：{$om['estimated_price']}，预估金额：{$om['estimated_amount']}，供应商：{$om['supplier_name']}\n";
        }
    } else {
        echo "- 暂无物料需求记录\n";
    }
    echo "\n=== 查询完成 ===\n";
    
} catch (PDOException $e) {
    echo "数据库连接失败：" . $e->getMessage() . "\n";
} catch (Exception $e) {
    echo "查询出错：" . $e->getMessage() . "\n";
}

?>